package edu.uic.kott.activities;

import java.util.List;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.fragments.DatePickerFragment;
import edu.uic.kott.model.ClassActivity;
import edu.uic.kott.model.Course;

import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;

public class AddEditActivity extends FragmentActivity implements DatePickerFragment.DatePickerDialogListener {

	private static final String LOGTAG = "KOTT";

	KottDataSource datasource; 

	List<Course> allCourses;

	private Spinner courseNameSpinner;
	private Spinner activityTypeSpinner;
	private EditText activityNameEditText;
	private EditText activityDescriptionEditText;
	private EditText dueDateEditText;
	private EditText maxGradeEditText;
	private CheckBox completedCheckBox;
	private EditText obtGradeEditText;

	private String id;
	private int courseId;
	private String activityType;
	private String activityName;
	private String activityDescription;
	private String dueDate;
	private String maxGrade;
	private String obtGrade;
	private String completed;
	private String option;
	private boolean requiredFields;	
	Bundle extras;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_edit_activity);

		datasource = new KottDataSource(this);
		allCourses = datasource.findAllCourses("AddEditActivity");

		courseNameSpinner = (Spinner) findViewById(R.id.activityClassSpinner);
		activityTypeSpinner = (Spinner) findViewById(R.id.activityTypeSpinner);
		activityNameEditText = (EditText) findViewById(R.id.newActivityNameEditText);
		activityDescriptionEditText = (EditText) findViewById(R.id.newActivityDesEditText);
		dueDateEditText = (EditText) findViewById(R.id.activityDueDateEditText);
		maxGradeEditText = (EditText) findViewById(R.id.maxGradeEditText);
		completedCheckBox = (CheckBox) findViewById(R.id.completedCheckBox);
		obtGradeEditText = (EditText)findViewById(R.id.gradeEditText);

		if(allCourses.size() > 0){	
			ArrayAdapter<Course> courseAdapter = new ArrayAdapter<Course>(this, android.R.layout.simple_spinner_item, allCourses);
			courseNameSpinner.setAdapter(courseAdapter);
			courseNameSpinner.setOnItemSelectedListener(courseListener);
		}

		ArrayAdapter<CharSequence> activityAdapter = ArrayAdapter.createFromResource(this, R.array.activity_type, android.R.layout.simple_spinner_item);
		//activityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		activityTypeSpinner.setAdapter(activityAdapter);
		activityTypeSpinner.setOnItemSelectedListener(activityListener);

		id= String.valueOf(0);
		courseId = 0;
		activityDescription = "Not Available";
		maxGrade = "N/A";
		obtGrade = "N/A";
		completed = "false";
		option = "add";

		if (extras != null){
			Log.i(LOGTAG,"Inside AddEdit for updating activity");

			option = extras.getString("option");

			Log.i(LOGTAG,"Option changed to " + option);			

			id = extras.getString("id");
			activityName = extras.getString("activityName");
			activityDescription = extras.getString("activityDescription");
			dueDate = extras.getString("dueDate");
			maxGrade = extras.getString("maxGrade");
			obtGrade = extras.getString("obtGrade");
			completed = extras.getString("completed");

			activityNameEditText.setText(activityName);
			dueDateEditText.setText(dueDate);

			if(!("Not Available".equalsIgnoreCase(activityDescription)))
				activityDescriptionEditText.setText(activityDescription);

			if(!("N/A".equalsIgnoreCase(maxGrade)))
				maxGradeEditText.setText(maxGrade);

			if(!("N/A".equalsIgnoreCase(obtGrade)))
				obtGradeEditText.setText(obtGrade);

			if(completed.equalsIgnoreCase("true"))
				completedCheckBox.setChecked(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_save, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		requiredFields = true;

		if(courseId == 0)
			requiredFields = false;

		if("- Select One -".equalsIgnoreCase(activityType))
			requiredFields = false;

		if(activityNameEditText.getText().toString().length() > 0)
			activityName = activityNameEditText.getText().toString();
		else requiredFields = false;

		if(dueDateEditText.getText().toString().length() > 0)
			dueDate = dueDateEditText.getText().toString();
		else requiredFields = false;

		if(activityDescriptionEditText.getText().toString().length() > 0)
			activityDescription = activityDescriptionEditText.getText().toString();

		if(maxGradeEditText.getText().toString().length() > 0)
			maxGrade = maxGradeEditText.getText().toString();

		if(obtGradeEditText.getText().toString().length() > 0)
			obtGrade = obtGradeEditText.getText().toString();

		if(completedCheckBox.isChecked())
			completed = "true";

		if(requiredFields == true)
		{
			Intent intent = new Intent(this, CourseActivities.class);
			ClassActivity acvty = new ClassActivity();
			acvty.setActivityId(Long.parseLong(id));
			acvty.setCourse(String.valueOf(courseId));
			acvty.setActivityType(activityType);
			acvty.setActivityName(activityName);
			acvty.setActivityDescription(activityDescription);
			acvty.setDueDate(dueDate);
			acvty.setMaxGrade(maxGrade);
			acvty.setObtGrade(obtGrade);
			acvty.setCompleted(completed);

			datasource.addUpdateActivity(acvty);

			/*if("update".equalsIgnoreCase(option)){
				Log.i(LOGTAG,"Call activityUpdate()");
				Log.i(LOGTAG,"Activity to be updated is " + String.valueOf(id));

				acvty.setActivityId(Long.parseLong(id));
				//datasource.updateActivity(acvty);
			}

			if("add".equalsIgnoreCase(option)){
				//long activityId = datasource.createActivity(acvty);
				//Log.i(LOGTAG,"Activity " + acvty.getActivityName() + " created with id " + activityId);
			}*/			

			startActivity(intent);
			finish();
		}
		else{
			AlertDialog.Builder builder = new AlertDialog.Builder(AddEditActivity.this);
			builder.setTitle(R.string.dialogTitle_requiredFields);	//set message title from string resource
			builder.setMessage(R.string.dialogMsg_requiredFields);//set message content
			builder.setPositiveButton("OK", null);		//ok button to dismiss the dialog box

			AlertDialog errorDialog = builder.create();
			errorDialog.show();
		}
		return super.onOptionsItemSelected(item);
	}

	AdapterView.OnItemSelectedListener courseListener = new AdapterView.OnItemSelectedListener() {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			Course course = (Course) parent.getItemAtPosition(position);
			courseId = (int) course.getCourseId();
			Log.i(LOGTAG,"Course id: " +  courseId + "\n Course name:  " + course.getCourseName());
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			courseId = 0;				
		}			
	};

	AdapterView.OnItemSelectedListener activityListener = new AdapterView.OnItemSelectedListener() {
		@Override
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			activityType = (String) parent.getItemAtPosition(position);
			Log.i(LOGTAG,"Type is " + activityType );
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			activityType = "N/A";				
		}			
	};

	public void InputDueDate(View v) {
		DialogFragment newFragment = DatePickerFragment.newInstance(1);
		newFragment.show(getFragmentManager(), "datePicker");
	}

	@Override
	public void onDateSet(int id, DatePicker view, int year, int month, int day) {
		dueDateEditText.setText(String.valueOf(month+1)+"/"+String.valueOf(day)+"/"+String.valueOf(year));

	}

}
